// 准确控制loading

import { ref } from "vue"

function useLoading(initial = false, delay = 300) {
  const loading = ref(initial);
  let timer: number;

  const startSync = () => {
    clearTimeout(timer);
    loading.value = true;
  }

  const end = () => {
    clearTimeout(timer);
    loading.value = false;
  }

  const start = () => {
    clearTimeout(timer);
    timer = window.setTimeout(() => {
      loading.value = true
    }, delay)
  }

  return {
    loading,
    startSync,
    start,
    end
  }
}

export {
  useLoading
}